<template>
  <div>
    <titleheader></titleheader>
    <div style="display: flex; flex-direction: row;">
      <slider></slider>
      
      <div style="margin-left: 20%; width: 80%; padding: 20px;">
        <!-- 统计信息 -->
        <div style="display: flex; margin-bottom: 20px;">
          <el-card shadow="hover" style="width: 200px; margin-right: 20px;">
            <div style="font-size: 24px; text-align: center;">{{ totalMembers }}</div>
            <div style="text-align: center;">总会员数</div>
          </el-card>
        </div>
        
        <!-- 搜索框 -->
        <el-input 
          v-model="searchQuery" 
          placeholder="搜索会员"
          style="width: 300px; margin-bottom: 20px;"
          @input="searchMembers">
        </el-input>
        
        <!-- 会员表格 -->
        <el-table :data="filteredMembers" border style="width: 100%">
          <el-table-column prop="memberNum" label="会员号"></el-table-column>
          <el-table-column prop="memberName" label="姓名"></el-table-column>
          <el-table-column prop="phoneNum" label="电话"></el-table-column>
          <el-table-column prop="card" label="健身卡"></el-table-column>
          <el-table-column prop="huiji" label="会籍"></el-table-column>
          <el-table-column label="操作">
            <template #default="scope">
              <el-button size="small" @click="editMember(scope.row)">编辑</el-button>
              <el-button size="small" type="danger" @click="deleteMember(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import axios from 'axios'
import slider from './slider.vue'
import titleheader from './titleheader.vue'

export default {
  components: { slider, titleheader },
  setup() {
    const members = ref([])
    const filteredMembers = ref([])
    const searchQuery = ref('')
    const totalMembers = ref(0)

    // 加载会员数据
    const loadMembers = async () => {
      const response = await axios.get('/api/members?action=list')
      members.value = response.data
      filteredMembers.value = response.data
      totalMembers.value = response.data.length
    }

    // 搜索会员
    const searchMembers = () => {
      if (!searchQuery.value) {
        filteredMembers.value = members.value
        return
      }
      const query = searchQuery.value.toLowerCase()
      filteredMembers.value = members.value.filter(member => 
        member.memberName.toLowerCase().includes(query) || 
        member.phoneNum.includes(query))
    }

    // 编辑会员
    const editMember = (member) => {
      // 打开编辑对话框
    }

    // 删除会员
    const deleteMember = async (member) => {
      try {
        await axios.delete(`/api/members/${member.memberNum}`)
        loadMembers()
      } catch (error) {
        console.error('删除失败', error)
      }
    }

    onMounted(() => {
      loadMembers()
    })

    return {
      members,
      filteredMembers,
      searchQuery,
      totalMembers,
      searchMembers,
      editMember,
      deleteMember
    }
  }
}
</script>